package com.yidu.controller;

import com.yidu.entity.DbCart;
import com.yidu.entity.DbProduct;
import com.yidu.service.DbProductService;
import com.yidu.utils.BaseMessage;
import com.yidu.utils.Tools;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * (DbProduct)表控制层
 *
 * @author makejava
 * @since 2021-07-30 09:18:58
 */
@RestController
@RequestMapping("dbProduct")
public class DbProductController {
    /**
     * 服务对象
     */
    @Resource
    private DbProductService dbProductService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectById")
    public DbProduct selectOne(Integer id) {
        return this.dbProductService.queryById(id);
    }

    @GetMapping("/list")
    public Map<String,Object> list(DbProduct product){
        List<DbProduct> list = this.dbProductService.queryAllByLimit(product);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",dbProductService.findCount(product));
        map.put("data",list);
        return map;
    }

    @PostMapping("/deleteAll")
    public BaseMessage deleteAll(String[] ids){
        //删除商品
        int rows = dbProductService.deleteAll(ids);
        BaseMessage bm = new BaseMessage(rows,"删除成功");
        return bm;
    }

    @GetMapping("/selectById")
    public Map selectById(Integer id){
        DbProduct product = this.dbProductService.queryById(id);
        Map map = new HashMap();
        if(product==null){
            map.put("state",0);
            map.put("msg","找不到该对象");
        }else {
            map.put("state",1);
            map.put("msg",product);
        }
        return map;
    }

    @PostMapping("/addOrUpdate")
    public BaseMessage addOrUpdate(
            @RequestParam(value = "fileImg",required = false)
                    MultipartFile file,//上传文件的类
            DbProduct product){
        String realPath = "D:\\4321\\fileUpload\\";
        String fileName = file.getOriginalFilename();
        if(!Tools.isEmpty(fileName)){
            try {
                String hz = fileName.substring(fileName.lastIndexOf("."));
                String fileNameNew = UUID.randomUUID().toString()+hz;
                //将上传的文件保存到硬盘中，文件的真实路径
                file.transferTo(new File(realPath+fileNameNew));
                //这是需要保存在数据库中的地址
                product.setPdphoto("/upload/"+fileNameNew);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //增加商品
        int rows = 0;
        if (product.getPdid()==null){
            rows = dbProductService.insert(product);
        }else {
            rows = dbProductService.update(product);
        }
        BaseMessage bm ;
        if(rows>0){
            bm = new BaseMessage(rows,"操作成功");
        }else {
            bm = new BaseMessage(rows,"操作失败");
        }

        return bm;
    }

//    @RequestMapping("/inCart")
//    @ResponseBody
//    public BaseMessage inCart(DbCart cartVo){
//        DbCart cart = dbProductService.queryById(cartVo);
//        Integer id = cartVo.getPdId();
//        Integer userId = cartVo.getUserId();
//        System.out.println(cart);
//        System.out.println("获得的PdID是："+id);
//        System.out.println("获得的userID是："+userId);
//        if(cart==null){
//            Product product = productDao.selectByPrimaryKey(id);
//            cart = new Cart();
//            cart.setPdId(product.getPdId());
//            cart.setCartMoney(product.getPdPrice());
//            cart.setCartNumber(1);
//            cart.setCartDate(Tools.parseDate(new Date()));
//            cart.setPdName(product.getPdName());
//            cart.setUserId(userId);
//            dao.insertSelective(cart);
//        }else {
//            System.out.println("商品单价："+cartVo.getPdPrice());
//            cart.setCartNumber(cart.getCartNumber()+1);
//            cart.setCartMoney(cartVo.getPdPrice()*cart.getCartNumber());
//            dao.updateByPrimaryKeySelective(cart);
//        }
//        sqlSession.commit();
//        sqlSession.close();
//        BaseMessage bm = new BaseMessage(1,"加入购物车成功");
//        return bm;
//    }


}
